package com.example.reagent_management.ui.home;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.lib_base.base.BaseDBFragment;
import com.example.lib_base.base.DataBindingConfig;
import com.example.lib_base.event.ShareViewModel;
import com.example.reagent_management.BR;
import com.example.reagent_management.R;
import com.example.reagent_management.databinding.FragmentHomeBinding;
import com.example.reagent_management.ui.home.adapter.HomeCountsAdapter;
import com.example.reagent_management.ui.home.adapter.HomeOperateAdapter;
import com.example.reagent_management.ui.home.pojo.OperateCounts;
import com.example.reagent_management.ui.operatedialog.dialogs.InStoreFragment;
import com.example.reagent_management.ui.operatedialog.dialogs.LockScreenFragment;
import com.example.reagent_management.ui.operatedialog.dialogs.OutStoreFragment;
import com.example.reagent_management.ui.operatedialog.dialogs.ReagentManagerFragment;

public class HomeFragment extends BaseDBFragment {
    private static final String TAG = "HomeFragment";

    private HomeCountsAdapter adapter;
    private HomeOperateAdapter operateAdapter;
    private HomeFragmentViewModel mState;
    private ShareViewModel mEvent;

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(HomeFragmentViewModel.class);
        mEvent = getApplicationScopeViewModel(ShareViewModel.class);
    }

    @Override
    protected void initViewOrObserve() {

        final FragmentHomeBinding binding = (FragmentHomeBinding) getBinding();
        binding.lockScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //锁屏
                new LockScreenFragment().showNow(getChildFragmentManager(),"LockScreenFragment");
            }
        });

        binding.dataList.setHasFixedSize(true);

        //拿初始化数据（要先注册观察者）
        //初始化架子数
        mState.requestList.getLists().observe(this, list -> {
            Log.e(TAG, "initViewOrObserve: " + list.size());
//            mState.operates.setValue(list);
            operateAdapter.setNewInstance(list);
//            binding.pageLayout.setGravityHelperAndAdapter(binding.dataList.getSnapHelper(),operateAdapter,6);
        });
        mState.requestList.requestList();
    }

    private long time = 0;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
//        adapter = new HomeCountsAdapter();
//        adapter.setDiffCallback(new DiffPositionCallBack());
//        adapter.setOnItemClickListener(new OnItemClickListener() {
//            @SuppressLint("RestrictedApi")
//            @Override
//            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
//                //不用safe args 用bundle 日常快速点击问题
//                long current = System.currentTimeMillis();
//                if (current >= time + 500) {
//                    time = current;
//                    Bundle bundle = new Bundle();
//                    bundle.putInt("position", ((PositionCounts) (adapter.getItem(position))).getPosition());
//                    try {
//                        nav().navigate(R.id.action_mainFragment_to_operateFragment, bundle);
//                    } catch (Exception ignored) {
//
//                    }
//                }
//                //navigation 插件问题 找不到direction
////                HomeFragmentDirections
//            }
//        });
        operateAdapter = new HomeOperateAdapter();
        operateAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                String content = ((OperateCounts) (adapter.getItem(position))).getContent();
                if (content != null) {
                    long current = System.currentTimeMillis();
                    if (current >= time + 500) {
                        time = current;
                        switch (position) {
                            case 0:
                                //InStoreFragment 每次点击应该新建一个，否则提取出来一直引用同一个可能会造成内存泄露
                                //不过有TAG标签在维护
                                //如果不每次创建一个，建议在Resume里面做判断是否还在
                                InStoreFragment inStoreFragment = new InStoreFragment();
                                inStoreFragment.showNow(getChildFragmentManager(), "InStoreFragment");
                                break;
                            case 1:
                                 new OutStoreFragment().showNow(getChildFragmentManager(),"OutStoreFragment");
                                break;
                            case 2:
                                new ReagentManagerFragment().showNow(getChildFragmentManager(),"ReagentManagerFragment");
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        });
        return new DataBindingConfig(R.layout.fragment_home, BR.vm, mState)
                .addBindingParam(BR.adapter, operateAdapter)
                .addBindingParam(BR.state, mEvent);
    }

}
